#include "XSystem/XCore.h"
#include "GInventory.h"

// Initialize the window
GInventory::GInventory(XResourcesManager *data) : XWindow(data)
{
	// Default values
    x_positionX = 185;
    x_positionY = 150;
    x_width = 236;
    x_height = 166;
	x_view.Move(-x_positionX, -x_positionY);

	x_windowBackground.SetImage(x_data->GetImage("gui/inventory.png"));
	sf::Sprite iconsSpr;
	iconsSpr.SetImage(x_data->GetImage("items/item1.png"));
	m_icons.push_back(iconsSpr);

	m_equips.resize(12, 0);
	m_invItemid.resize(12, 0);
	m_invQuantity.resize(12, 0);
}

// Draws the window
void GInventory::Draw()
{
    if (x_visible)
    {
        XWindow::Draw();

		// Draws the equipped items
		for (int i=0; i<12; i++)
		{
			int ic100 = m_equips[i] / 100;
			int ic10 = m_equips[i] / 10;
			int ic1 = m_equips[i] % 10;

			int ihori = i / 3;
			int ivert = i % 3;

			m_icons[ic100].SetSubRect(sf::IntRect(32*ic1, 32*ic10, 32*ic1+32, 32*ic10+32)); 
			m_icons[ic100].SetPosition((ivert * 37) + 6, (ihori * 37) + 17);

			x_app->Draw(m_icons[ic100]);
		}

		// Draws the inventory items
		for (int i=0; i<12; i++)
		{
			int ic100 = m_invItemid[i] / 100;
			int ic10 = m_invItemid[i] / 10;
			int ic1 = m_invItemid[i] % 10;

			int ihori = i / 3;
			int ivert = i % 3;

			m_icons[ic100].SetSubRect(sf::IntRect(32*ic1, 32*ic10, 32*ic1+32, 32*ic10+32)); 
			m_icons[ic100].SetPosition((ivert * 37) + 123, (ihori * 37) + 17);

			x_app->Draw(m_icons[ic100]);
		}
    }
}

// Refresh the window
void GInventory::CheckState(const sf::Input &input)
{
	XWindow::CheckState(input);
}

// Check the queries of the widgets
void GInventory::CheckQuery()
{
	if (!x_data->queueEmpty("items"))
	{
		functions function = x_data->nextQueue("items");
		std::string items = function.variable;

		if (function.argument == "equips")
		{
			int lastcheck = 0;
			int checkpos = 0;
			bool done = false;

			for (int i=0; i<10; i++)
			{
				done = false;
				while (!done)
				{
					if (items[checkpos] == 'n')
					{
						std::string t_substr = items.substr(lastcheck,  (checkpos - lastcheck));
						done = true;
						int equip = atoi(t_substr.c_str());

						std::stringstream ss;
						ss << "equip_" << i;

						m_equips[i] = equip;
						x_data->SetInt(ss.str(), equip);

						lastcheck = checkpos + 1;
					}

					checkpos++;
				}
			}
			
		} else if (function.argument == "inventory")
		{
			int itemid, quantity, invid;
			int lastcheck = 0;
			int checkpos = 0;
			bool done = false;

			while (lastcheck < items.size()-1)
			{
				done = false;
				while (!done)
				{
					if (items[checkpos] == 'i')
					{
						std::string t_substr = items.substr(lastcheck,  (checkpos - lastcheck));
						done = true;
						quantity = atoi(t_substr.c_str());
						lastcheck = checkpos + 1;
					}

					checkpos++;
				}

				done = false;
				while (!done)
				{
					if (items[checkpos] == 'a')
					{
						std::string t_substr = items.substr(lastcheck,  (checkpos - lastcheck));
						done = true;
						itemid = atoi(t_substr.c_str());
						lastcheck = checkpos + 1;
					}

					checkpos++;
				}

				done = false;
				while (!done)
				{
					if (items[checkpos] == 'n')
					{
						std::string t_substr = items.substr(lastcheck,  (checkpos - lastcheck));
						done = true;
						invid = atoi(t_substr.c_str());
						lastcheck = checkpos + 1;
					}

					checkpos++;
				}

				m_invItemid[invid] = itemid;
				m_invQuantity[invid] = quantity;
			}
		}
	}
}
